package com.bank.service;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Date;
import javax.annotation.Resource;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

@Service("pdfService")
@Transactional
public class PdfService {

	@Resource(name="sessionFactory")
	private SessionFactory sessionFactory;
	
    public String createPDF(Integer userCode, String name, String surname, String email, java.util.List <String> codes){
   	 try {
         //OutputStream file = new FileOutputStream(new File("../webapps/ROOT/resources/pdf/profile-"+userCode+".pdf"));
   		OutputStream file = new FileOutputStream(new File("src\\main\\webapp\\resources\\pdf\\profile-"+userCode+".pdf"));
         Document document = new Document();
         PdfWriter.getInstance(document, file);
         PdfPTable table=new PdfPTable(2);
         PdfPCell cell = new PdfPCell (new Paragraph ("User code card"));
         cell.setColspan (2);
         cell.setHorizontalAlignment (Element.ALIGN_CENTER);
         cell.setPadding (3.0f);
         cell.setBackgroundColor (new BaseColor (140, 221, 8));                                   
         table.addCell(cell);                                     
         int count = 0;
		 for (String code : codes) {
			count++;
	         table.addCell(""+count);
	         table.addCell(""+code);			
		 }
         table.setSpacingBefore(10.0f);       // Space Before table starts, like margin-top in CSS
         table.setSpacingAfter(10.0f);        // Space After table starts, like margin-Bottom in CSS                                         
         //Now Insert Every Thing Into PDF Document
         document.open();//PDF document opened........                 
         document.add(Chunk.NEWLINE);   //Something like in HTML <img src="http://www.java4s.com/wp-includes/images/smilies/icon_smile.gif" alt=":-)" class="wp-smiley"> 
         document.add(new Paragraph("Document created at - "+new Date().toString()));  
         document.add(new Paragraph("Name: " + name));
         document.add(new Paragraph("Surname: " + surname));
         document.add(new Paragraph("Email: " + email));
         document.add(new Paragraph("User code: " + userCode));
         document.add(table);
         document.add(Chunk.NEWLINE);   //Something like in HTML <img src="http://www.java4s.com/wp-includes/images/smilies/icon_smile.gif" alt=":-)" class="wp-smiley">                                 
         document.newPage();            //Opened new page
         document.close();
         file.close();
         System.out.println("Pdf created successfully..");

   } catch (Exception e) {
       e.printStackTrace();
   }
   	 return "profile-"+userCode+".pdf";
    }
}